//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "UKRPI.h"
using namespace Cephei::QL::Indexes::Inflation;
#include <gen/QL/Termstructures/ZeroInflationTermStructure.h>
#include <gen/QL/Indexes/Region.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Currency.h>
#include <gen/QL/Indexes/ZeroInflationIndex.h>
#include <gen/QL/Times/Calendar.h>
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL::Times;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Indexes::Inflation::CUKRPI::CUKRPI (Boolean interpolated, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IZeroInflationTermStructure^>^ ts) : CZeroInflationIndex(CUKRPI::typeid)
{
    CZeroInflationTermStructure^ _Cts;
    try
    {
#ifdef HANDLE
        _phUKRPI = NULL;
#endif
        bool _interpolated = (bool)ValueHelper::Convert (interpolated); //d
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IZeroInflationTermStructure^>::IsSome::get (ts))
        {
            _Cts = safe_cast<CZeroInflationTermStructure^> (ts->Value);
            _Cts->Lock();
        }
        Handle<QuantLib::ZeroInflationTermStructure>& _ts = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IZeroInflationTermStructure^>::IsSome::get (ts) ? static_cast<Handle<QuantLib::ZeroInflationTermStructure>&> (_Cts->GetHandle ()) : Handle<QuantLib::ZeroInflationTermStructure>()); //1
        _ppUKRPI = new boost::shared_ptr<QuantLib::UKRPI> (new QuantLib::UKRPI ( _interpolated,  _ts ));
        SetZeroInflationIndex (boost::dynamic_pointer_cast<QuantLib::ZeroInflationIndex> (*_ppUKRPI));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cts != nullptr) _Cts->Unlock();
    }
}
Cephei::QL::Indexes::Inflation::CUKRPI::CUKRPI (boost::shared_ptr<QuantLib::UKRPI>& childNative, Object^ owner) : CZeroInflationIndex(CUKRPI::typeid)
{
#ifdef HANDLE
	_phUKRPI = NULL;
#endif
	_ppUKRPI = &childNative;
    _ppZeroInflationIndex = new boost::shared_ptr<QuantLib::ZeroInflationIndex> (boost::dynamic_pointer_cast<QuantLib::ZeroInflationIndex> (*_ppUKRPI));
}
Cephei::QL::Indexes::Inflation::CUKRPI::CUKRPI (QuantLib::UKRPI& childNative, Object^ owner) : CZeroInflationIndex(CUKRPI::typeid)
{
#ifdef HANDLE
	_phUKRPI = NULL;
#endif
	_ppUKRPI = new boost::shared_ptr<QuantLib::UKRPI> (&childNative);
    _ppZeroInflationIndex = new boost::shared_ptr<QuantLib::ZeroInflationIndex> (boost::dynamic_pointer_cast<QuantLib::ZeroInflationIndex> (*_ppUKRPI));
    _UKRPIOwner = owner;
    _ZeroInflationIndexOwner = owner;
}

Cephei::QL::Indexes::Inflation::CUKRPI::CUKRPI (CUKRPI^ copy) : CZeroInflationIndex(CUKRPI::typeid)
{
#ifdef HANDLE
	_phUKRPI = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppUKRPI = new boost::shared_ptr<QuantLib::UKRPI> (copy->GetShared());
        _ppZeroInflationIndex = new boost::shared_ptr<QuantLib::ZeroInflationIndex> (boost::dynamic_pointer_cast<QuantLib::ZeroInflationIndex> (*_ppUKRPI));
    }
}
Cephei::QL::Indexes::Inflation::CUKRPI::CUKRPI (PLATFORM::Type^ t) : CZeroInflationIndex(CUKRPI::typeid)
{
#ifdef HANDLE
	_phUKRPI = NULL;
#endif
	if (!t->IsSubclassOf(CUKRPI::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Indexes::Inflation::CUKRPI::CUKRPI (QuantLib::Handle<QuantLib::UKRPI>& childNative, Object^ owner)  : CZeroInflationIndex(CUKRPI::typeid)
{
	_phUKRPI = &childNative;
	_ppUKRPI = &static_cast<boost::shared_ptr<QuantLib::UKRPI>>(childNative.currentLink());
    _ppZeroInflationIndex = new boost::shared_ptr<QuantLib::ZeroInflationIndex> (boost::dynamic_pointer_cast<QuantLib::ZeroInflationIndex> (*_ppUKRPI));
    _UKRPIOwner = owner;
}
Cephei::QL::Indexes::Inflation::CUKRPI::CUKRPI (QuantLib::Handle<QuantLib::UKRPI> childNative)  : CZeroInflationIndex(CUKRPI::typeid)
{
	_phUKRPI = &childNative;
	_ppUKRPI = &static_cast<boost::shared_ptr<QuantLib::UKRPI>>(childNative.currentLink());
    _ppZeroInflationIndex = new boost::shared_ptr<QuantLib::ZeroInflationIndex> (boost::dynamic_pointer_cast<QuantLib::ZeroInflationIndex> (*_ppUKRPI));
}
#endif
#ifdef STRUCT
Cephei::QL::Indexes::Inflation::CUKRPI::CUKRPI (QuantLib::UKRPI childNative)  : CZeroInflationIndex(CUKRPI::typeid)
{
#ifdef HANDLE
	_phUKRPI = NULL;
#endif
	_ppUKRPI = new boost::shared_ptr<QuantLib::UKRPI> (new QuantLib::UKRPI (childNative));
    _ppZeroInflationIndex = new boost::shared_ptr<QuantLib::ZeroInflationIndex> (boost::dynamic_pointer_cast<QuantLib::ZeroInflationIndex> (*_ppUKRPI));
}
#endif

Cephei::QL::Indexes::Inflation::CUKRPI::~CUKRPI ()
{
    if (_ppUKRPI != NULL)
    {
	    delete _ppUKRPI;
        _ppUKRPI = NULL;
    }
}
Cephei::QL::Indexes::Inflation::CUKRPI::!CUKRPI ()
{
    if (_ppUKRPI != NULL)
    {
	    delete _ppUKRPI;
    }
}
QuantLib::UKRPI& Cephei::QL::Indexes::Inflation::CUKRPI::GetReference ()
{
    if (_ppUKRPI == NULL) throw REFNEW NativeNullException ();
	return **_ppUKRPI;
}
boost::shared_ptr<QuantLib::UKRPI>& Cephei::QL::Indexes::Inflation::CUKRPI::GetShared ()
{
    if (_ppUKRPI == NULL) throw REFNEW NativeNullException ();
	return *_ppUKRPI;
}
QuantLib::UKRPI* Cephei::QL::Indexes::Inflation::CUKRPI::GetPointer ()
{
    if (_ppUKRPI == NULL) throw REFNEW NativeNullException ();
	return &**_ppUKRPI;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::UKRPI>& Cephei::QL::Indexes::Inflation::CUKRPI::GetHandle ()
{
	if (_phUKRPI == NULL)
	{
		_phUKRPI = new Handle<QuantLib::UKRPI> (*_ppUKRPI);
	}
	return *_phUKRPI;
}
#endif
bool Cephei::QL::Indexes::Inflation::CUKRPI::HasNative () 
{
	return (_ppUKRPI != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Indexes::Inflation::IUKRPI^ Cephei::QL::Indexes::Inflation::CUKRPI_Factory::Create (Boolean interpolated, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IZeroInflationTermStructure^>^ ts)
{
    return REFNEW CUKRPI ( interpolated,  ts);
}
